import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/diolog/dialog.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/model/mine/points_redemption_model.dart';
import 'package:pornhub_app/model/mine/signIn_mission_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/views/main/controllers/main_controller.dart';

import 'welfare_page_controller.dart';

///福利任务
class WelfarePage extends GetView<WelfarePageController> {
  const WelfarePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBodyView(),
    );
  }

  _buildBodyView() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: SingleChildScrollView(
        child: Column(
          children: [
            ///任务
            20.verticalSpace, _buildTaskView(),

            ///积分
            20.verticalSpace, _buildIntegralView(),
          ],
        ),
      ),
    );
  }

  ///任务
  _buildTaskView() {
    return AppBgView(
      width: double.infinity,
      backgroundColor: Colors.white,
      radius: 6.w,
      padding: EdgeInsets.all(14.w),
      child: Column(
        children: [
          Row(
            children: [
              TextView(
                text: "做任务领取奖励",
                color: COLOR.color_333333,
                fontSize: 16.w,
                fontWeight: FontWeight.w600,
              ),
              const Spacer(),
              TextView(
                text: "每日0点刷新",
                color: COLOR.color_666666,
                fontSize: 12.w,
              ),
            ],
          ),
          15.verticalSpace,
          Obx(() => ListView.separated(
                shrinkWrap: true,
                itemCount: controller.missions.length,
                padding: EdgeInsets.zero,
                itemBuilder: (BuildContext context, int index) {
                  return _buildTaskItemView(controller.missions[index]);
                },
                separatorBuilder: (BuildContext context, int index) {
                  return 10.verticalSpace;
                },
              )),
        ],
      ),
    );
  }

  ///任务Item
  _buildTaskItemView(SigninMissionModel model) {
    return AppBgView(
      width: double.infinity,
      backgroundColor: COLOR.color_E5E5E5,
      radius: 8.w,
      padding: EdgeInsets.symmetric(horizontal: 11.w, vertical: 8.w),
      child: Row(
        children: [
          ImageView(
            src: model.missionLogo ?? "",
            width: 33.w,
            height: 33.w,
          ),
          5.horizontalSpace,
          Expanded(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text.rich(
                TextSpan(
                  children: [
                    TextSpan(
                        text: model.missionName ?? '',
                        style: TextStyle(
                            color: COLOR.color_333333, fontSize: 12.w)),
                    TextSpan(
                      text: '+${model.integral}积分',
                      style:
                          TextStyle(color: COLOR.color_B93FFF, fontSize: 10.w),
                    ),
                  ],
                ),
              ),
              TextView(
                text: model.missionDesc ?? '',
                color: COLOR.color_999999,
                fontSize: 11.w,
              ),
            ],
          )),
          5.horizontalSpace,
          AppBgView(
            width: 60.w,
            height: 22.w,
            imagePath: model.status == 3
                ? AppImagePath.btn_bg_grey
                : model.status == 2
                    ? AppImagePath.btn_bg_1
                    : AppImagePath.btn_line,
            fit: BoxFit.fill,
            text: model.status == 3
                ? "已领取"
                : model.status == 2
                    ? "待领取"
                    : "去完成",
            textColor: model.status == 1 ? COLOR.color_B93FFF : COLOR.white,
            textSize: 11.w,
            onTap: () => controller.startMissions(model),
          ),
        ],
      ),
    );
  }

  ///积分
  _buildIntegralView() {
    return Column(
      children: [
        Row(
          children: [
            TextView(
              text: "积分兑换",
              color: COLOR.white,
              fontSize: 16.w,
              fontWeight: FontWeight.w600,
            ),
            const Spacer(),
            TextView(
              text: "我的积分：",
              color: COLOR.white,
              fontSize: 14.w,
            ),
            Obx(() => TextView(
                  text: "${controller.exchangeIntegral.value.integral ?? 0}",
                  color: COLOR.color_DD001B,
                  fontSize: 14.w,
                )),
            18.horizontalSpace,
            GestureDetector(
              onTap: () {
                showAlertDialog(
                  Get.context!,
                  backgroundColor: COLOR.white,
                  titleWidget: TextView(
                    text: "签到规则",
                    color: COLOR.color_333333,
                    fontSize: 16.w,
                  ),
                  leftText: "在线客服",
                  leftTextColor: COLOR.black,
                  leftBgColor: COLOR.color_E5E5E5,
                  rightText: "知道了",
                  onLeftButton: () {
                    kOnLineService();
                  },
                  content: TextView(
                    text: "1.每日签到和做任务可获得相应积分。"
                        "\n2.使用过程中不找零、不兑现、不开发票、不可转移至 其他账户。"
                        "\n3.使用积分进行兑换，兑换申请一经提交，一律不能取消 （除奖品缺货等特殊情况）"
                        "\n4.实物奖品会通过快递15个工作日内完成发货，因地址不对造成无法收到快递，平台不会补寄奖品。"
                        "\n5.所有发货奖品包邮，不收取兑换人任何费用。"
                        "\n6.积分有效期为1年，过期自动清理。"
                        "\n7.平台保留最终解释权",
                    color: COLOR.color_666666,
                    fontSize: 11.w,
                    textAlign: TextAlign.start,
                  ),
                );
              },
              child: TextView(
                text: "签到规则",
                style: TextStyle(
                  color: COLOR.white,
                  fontSize: 14.w,
                  decoration: TextDecoration.underline,
                  decorationColor: Colors.white,
                  decorationThickness: 1,
                ),
              ),
            ),
          ],
        ),
        13.verticalSpace,
        Obx(() => GridView.builder(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              padding: EdgeInsets.only(top: 14.w),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  crossAxisSpacing: 10.w,
                  mainAxisSpacing: 12.w,
                  childAspectRatio: 179 / 85),
              itemCount: controller.prizes.length,
              itemBuilder: (BuildContext context, int index) {
                return _buildIntegralItemView(controller.prizes[index]);
              },
            ))
      ],
    );
  }

  _buildIntegralItemView(PrizeList prize) {
    return AppBgView(
      width: double.infinity,
      height: 85.w,
      backgroundColor: COLOR.color_E9EFFC,
      radius: 6.w,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            flex: 2,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              child: Row(
                children: [
                  Expanded(
                      child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      TextView(
                        text: prize.prizeName ?? "",
                        color: COLOR.color_333333,
                        fontSize: 14.w,
                        fontWeight: FontWeight.w700,
                      ),
                      TextView(
                        text: prize.desc ?? '',
                        color: COLOR.color_333333,
                        fontSize: 11.w,
                      ),
                    ],
                  )),
                  ImageView(
                    src: prize.image ?? '',
                    width: 40.w,
                    height: 40.w,
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            flex: 1,
            child: AppBgView(
              height: 85.w,
              backgroundColor: COLOR.white,
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(6.w),
                bottomRight: Radius.circular(6.w),
              ),
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              child: Row(
                children: [
                  TextView(
                    text: "花费${prize.needIntegral}积分",
                    color: COLOR.color_333333,
                    fontSize: 11.w,
                  ),
                  const Spacer(),
                  AppBgView(
                    text: "立即兑换",
                    height: 17.w,
                    radius: 9.w,
                    textSize: 9.w,
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    backgroundColor: COLOR.color_F52443,
                    textColor: COLOR.white,
                    onTap: () {
                      controller.exChangePrize(prize);
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
